﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint.LookupPicker (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using Res = Sapiens.at.SharePoint.Fields.Resources.Sapiens_at_SharePoint_LookupPicker;

namespace Sapiens.at.SharePoint.Fields
{
    public static class LookupEditorDataCollector
    {
        internal static void FillWebsList(ListControl webs)
        {
            webs.DataSource = SPContext.Current.Site.AllWebs;
            webs.DataTextField = "Title";
            webs.DataValueField = "ID";
            webs.DataBind();
        }

        internal static void FillListsList(ListControl lists, string webID)
        {
            if (string.IsNullOrEmpty(webID)) { return; }
            lists.DataSource = SPContext.Current.Site.AllWebs[new Guid(webID)].Lists;
            lists.DataTextField = "Title";
            lists.DataValueField = "ID";
            lists.DataBind();
        }

        internal static void FillWithList(
            ListControl mainDisplayFields,
            ListControl additionalSearchColumns,
            ListControl additionalDisplayColumns,
            ListControl contentTypes,
            string webID,
            string listID)
        {
            if (string.IsNullOrEmpty(webID) || string.IsNullOrEmpty(listID)) { return; }

            var list = SPContext.Current.Site.AllWebs[new Guid(webID)].Lists[new Guid(listID)];
            var fields = GetDisplayColumns(list);

            BindFields(mainDisplayFields, fields);
            BindFields(additionalSearchColumns, fields);
            BindFields(additionalDisplayColumns, fields);

            contentTypes.DataSource = list.ContentTypes;
            contentTypes.DataTextField = "Name";
            contentTypes.DataValueField = "Id";
            contentTypes.DataBind();
        }

        private static void BindFields(ListControl control, IEnumerable<SPField> fields)
        {
            if (control == null) { return; }
            control.Items.Clear();
            foreach (SPField field in fields)
            {
                control.Items.Add(new ListItem(String.Concat(field.Title, " ", field.AuthoringInfo), field.InternalName));
            }
        }

        internal static IEnumerable<SPField> GetDisplayColumns(SPList list)
        {
            foreach (SPField field in list.Fields)
            {
                if (field.Hidden == false)
                {
                    switch (field.Type)
                    {
                        case SPFieldType.Calculated:
                            if (((SPFieldCalculated)field).OutputType == SPFieldType.Text)
                            {
                                yield return field;
                            }
                            break;
                        case SPFieldType.Computed:
                            if (((SPFieldComputed)field).EnableLookup)
                            {
                                yield return field;
                            }
                            break;
                        case SPFieldType.Counter:
                        case SPFieldType.Text:
                            yield return field;
                            break;
                    }
                }
            }
        }

        internal static void FillSearchScope(ListControl searchScope)
        {
            searchScope.Items.Clear();
            searchScope.Items.Add(new ListItem(Res.ScopeRecursiveAll, "RecursiveAll"));
            searchScope.Items.Add(new ListItem(Res.ScopeNoFolders, "Recursive"));
            searchScope.Items.Add(new ListItem(Res.ScopeRootLevelItems, ""));
            searchScope.Items.Add(new ListItem(Res.ScopeFilesOnly, "FilesOnly"));   
        }
    }
}
